package com.manage.tasks;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import com.manage.func.article.dao.IArticleDao;
import com.manage.func.article.model.ArticleExpireBean;
import com.manage.util.SpringApplicationContextHolder;

/**
 * 新闻过期状态检测线程
 * 
 * @author suid
 *
 */
public class NewsCheckTask extends TimerTask {

	String name;
	public boolean runFlag = true;

	private IArticleDao newsDao;

	@Override
	public void run() {
		try {
			newsDao = SpringApplicationContextHolder.getSpringBeanByType(IArticleDao.class);
			while (newsDao == null) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
				newsDao = SpringApplicationContextHolder.getSpringBeanByType(IArticleDao.class);
			}
			
			if (runFlag) {
				newsExpireCheck();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据当前时间来判断新闻的过期状态
	 */
	private void newsExpireCheck() {

		Long curr = System.currentTimeMillis();
		Long expire = 0L;
		List<Long> expireIds = new ArrayList<Long>();
		List<ArticleExpireBean> lists = newsDao.expireSelect();
		if (lists != null && !lists.isEmpty()) {
			for (ArticleExpireBean bean : lists) {
				if (bean.expireTime != null && !bean.expireTime.equals("")) {
					expire = Timestamp.valueOf(bean.expireTime).getTime();
					if (curr > expire) {
						expireIds.add(bean.articleId);
					}
				}
			}
		}
		for (Long exId : expireIds) {
			newsDao.expireUpdate(exId);
		}

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	

}
